package jms;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQPrefetchPolicy;
import org.apache.activemq.RedeliveryPolicy;

public class JMSConsumer {

	public static void main(String[] args) throws JMSException {
		ActiveMQConnectionFactory connectionFactory;// 连接工厂
		Connection connection = null;// 连接
		ActiveMQPrefetchPolicy p = new ActiveMQPrefetchPolicy();
		p.setQueuePrefetch(1);
		Session session;// 会话 接受或者发送消息的线程
		Destination destination;// 消息的目的地
       
		MessageConsumer messageConsumer;// 消息的消费者

		// 实例化连接工厂
		connectionFactory = new ActiveMQConnectionFactory("system", "zdc524", "tcp://IP:61616");
		connectionFactory.setPrefetchPolicy(p);
		connectionFactory.setTrustAllPackages(true);
		// 通过连接工厂获取连接
		
		RedeliveryPolicy  p1 = new RedeliveryPolicy();
		p1.setMaximumRedeliveries(3);
		//p1.setRedeliveryDelay(100);
		connectionFactory.setRedeliveryPolicy(p1);
		connectionFactory.setOptimizeAcknowledge(false);
		//connectionFactory.setp
		// 启动连接
		connection = connectionFactory.createConnection();
		connection.start();
		// connection
		// 创建session
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE );
		// 创建一个连接HelloWorld的消息队列
		destination = session.createQueue("HelloWorld");
	

		// 创建消息消费者
		messageConsumer = session.createConsumer(destination);
		Listener listener = new Listener();
		messageConsumer.setMessageListener(listener);
	
		 while (true) {
             
         }

	}

}
